﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using StoreTransport.Business.Constants;
using StoreTransport.DAO;
using StoreTransport.Data;
using StoreTransport.Resources;
using StoreTransport.Business.Helper;

namespace StoreTransport.Presentation.Forms {
    public partial class ProductKindListForm : Form {
        int id = 0;
        Dictionary<string, string> productLineDic = new Dictionary<string,string>();
        public ProductKindListForm() {
            InitializeComponent();
        }

        private void _btxCreate_Click(object sender, EventArgs e) {
            _drpProductLine.Select();
            _lblID.Text = StoreTransportResource.Form_NewID;
            _tbxProductKindMainType.Text = string.Empty;
            _tbxProductKindSubType1.Text = string.Empty;
            _tbxProductKindSubType2.Text = string.Empty;
            _tbxProductKindSubType3.Text = string.Empty;
            _tbxProductKindSubType4.Text = string.Empty;
            _tbxProductKindName.Text = string.Empty;
            _tbxProductRecieve.Text = string.Empty;
            CommonHelper.ResetCombobox(_drpProductLine);
            id = 0;
            LoadProductKindList();
        }

        private void LoadProductKindList() {
            if ((_drpProductLine.Items.Count == 0) || (_drpProductLine.SelectedItem as ProductLine) == null) {
                return;
            }
            int prodcutlineID = (_drpProductLine.SelectedItem as ProductLine).ID;
            _grdProductKind.AutoGenerateColumns = false;
            var productKindList = CommonHelper.GetContext().ProductKinds.Where(c => c.ProductLine == prodcutlineID).Cast<ProductKind>().
                OrderBy(c => c.ProductKindName);
            List<PresentProductKindDTO> pProductKindList = new List<PresentProductKindDTO>();
            foreach (ProductKind pk in productKindList) {
                string productLine = string.Empty;
                if (productLineDic.ContainsKey(pk.ProductLine.ToString())) {
                    productLine = productLineDic[pk.ProductLine.ToString()];
                }
                string recieverProducts = pk.NumberReceiveProduct.HasValue ? pk.NumberReceiveProduct.Value.ToString() : string.Empty;
                pProductKindList.Add(new PresentProductKindDTO(pk.ID, pk.ProductLine1.ProductLineName, pk.ProductKindName, productLine,
                    pk.MainType, pk.SubType1, pk.SubType2, pk.SubType3, pk.SubType4, recieverProducts));
            }
            _grdProductKind.DataSource = pProductKindList.OrderBy(c => c.ProductLineName).
                ThenBy(c => c.ProductKindName).ToList();
        }

        private void _btnDelete_Click(object sender, EventArgs e) {
            if (_grdProductKind.SelectedRows.Count > 0) {
                DialogResult result = MessageBox.Show(StoreTransportResource.Message_ConfirmDelete,
                    StoreTransportResource.MessageBox_ConfirmDelete, MessageBoxButtons.YesNo);
                if (result == System.Windows.Forms.DialogResult.Yes) {
                    StoreTransportDataContext context = CommonHelper.GetContext();
                    foreach (DataGridViewRow row in _grdProductKind.SelectedRows) {
                        id = int.Parse(row.Cells[0].Value.ToString());
                        ProductKind selectedItem = context.ProductKinds.Where(c => c.ID == id).First();
                        context.ProductKinds.DeleteOnSubmit(selectedItem);
                        context.SubmitChanges();
                    }
                    id = 0;
                    LoadProductKindList();
                }
            } else {
                MessageBox.Show(StoreTransportResource.NoItemSelected);
            }
        }

        private void _btxEdit_Click(object sender, EventArgs e) {
            if (_grdProductKind.SelectedRows.Count > 0) {
                id = int.Parse(_grdProductKind.SelectedRows[0].Cells[0].Value.ToString());
                EditItem();
            }
        }

        private void EditItem() {
            ProductKind selectedItem = CommonHelper.GetContext().
                ProductKinds.Where(c => c.ID.Equals(id)).First();
            _lblID.Text = selectedItem.ID.ToString();
            _tbxProductKindMainType.Text = selectedItem.MainType;
            _tbxProductKindSubType1.Text = selectedItem.SubType1;
            _tbxProductKindSubType2.Text = selectedItem.SubType2;
            _tbxProductKindSubType3.Text = selectedItem.SubType3;
            _tbxProductKindSubType4.Text = selectedItem.SubType4;
            _tbxProductRecieve.Text = selectedItem.NumberReceiveProduct.HasValue ? selectedItem.NumberReceiveProduct.Value.ToString() : string.Empty;
            _tbxProductKindName.Text = selectedItem.ProductKindName;
            CommonHelper.SetComboboxValue(_drpProductLine, selectedItem.ProductLine);
        }

        private void _btxSave_Click(object sender, EventArgs e) {
            string errorMessage = CommonHelper.CheckRequiredField(_tbxProductKindName.Text, _lblProductKindName.Text);
            if (!string.IsNullOrEmpty(_tbxProductRecieve.Text)) {
                errorMessage += CommonHelper.CheckNumberValue(_tbxProductRecieve.Text, _lblProductRecieve.Text);
            }
            if (!string.IsNullOrEmpty(errorMessage)) {
                MessageBox.Show(errorMessage, StoreTransportResource.ErrorMessage_ErrorMessageTitle);
                return;
            }
            StoreTransportDataContext context = CommonHelper.GetContext();
            int productLineID = int.Parse(_drpProductLine.SelectedValue.ToString());
            ProductKind pk = new ProductKind();
            if (id != 0) {
                var item = context.ProductKinds.FirstOrDefault(c => c.ProductKindName.Equals(_tbxProductKindName.Text) &&
                    c.ProductLine == productLineID);
                if (item != null && item.ID != id) {
                    MessageBox.Show(string.Format(StoreTransportResource.Error_Dupplicate_ProductKind,
                        _tbxProductKindName.Text, _drpProductLine.Text));
                    return;
                }
                pk = context.ProductKinds.Where(c => c.ID == id).First();
            } else {
                if (context.ProductKinds.FirstOrDefault(c => c.ProductKindName.Equals(_tbxProductKindName.Text) &&
                    c.ProductLine == productLineID) != null) {
                    MessageBox.Show(string.Format(StoreTransportResource.Error_Dupplicate_ProductKind,
                        _tbxProductKindName.Text, _drpProductLine.Text));
                    return;
                }
                context.ProductKinds.InsertOnSubmit(pk);
            }
            pk.ProductKindName = _tbxProductKindName.Text;
            pk.MainType = _tbxProductKindMainType.Text;
            pk.SubType1 = _tbxProductKindSubType1.Text;
            pk.SubType2 = _tbxProductKindSubType2.Text;
            pk.SubType3 = _tbxProductKindSubType3.Text;
            pk.SubType4 = _tbxProductKindSubType4.Text;
            if (!string.IsNullOrEmpty(_tbxProductRecieve.Text)) {
                pk.NumberReceiveProduct = int.Parse(_tbxProductRecieve.Text);
            } else {
                pk.NumberReceiveProduct = 0;
            }
            pk.ProductLine = productLineID;
            context.SubmitChanges();
            LoadProductKindList();
        }

        private void ProductKindListForm_Load(object sender, EventArgs e) {
            _drpProductLine.DisplayMember = ColumnName.ProductLineName;
            _drpProductLine.ValueMember = ColumnName.ID;
            StoreTransportDataContext context = CommonHelper.GetContext();
            _drpProductLine.DataSource = context.ProductLines.OrderBy(c => c.ProductLineName);
            productLineDic = context.ProductLines.ToDictionary(c => c.ID.ToString(), c => c.ProductLineName);
            _drpProductLine.Select();
        }

        private void _grdProductKind_CellDoubleClick(object sender, DataGridViewCellEventArgs e) {
            id = int.Parse(_grdProductKind.Rows[e.RowIndex].Cells[0].Value.ToString());
            EditItem();
        }

        private void _drpProductLine_SelectedIndexChanged(object sender, EventArgs e) {
            LoadProductKindList();
        }
    }
}
